package com.wanmait.schedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.schedule.entity.Role;
import com.wanmait.schedule.entity.Teacher;
import com.wanmait.schedule.entity.TeacherRole;
import com.wanmait.schedule.mapper.RoleMapper;
import com.wanmait.schedule.mapper.TeacherMapper;
import com.wanmait.schedule.mapper.TeacherRoleMapper;
import com.wanmait.schedule.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 教师表 服务实现类
 * </p>
 *
 * @author wanmait
 * @since 2022-12-05
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Resource
    private TeacherMapper teacherMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private TeacherRoleMapper teacherRoleMapper;

    @Override
    public PageInfo<Teacher> list(Teacher teacher, Integer pageNum) {
        if(pageNum==null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,6);
        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(teacher.getName()),Teacher::getName,teacher.getName());
        queryWrapper.like(StringUtils.isNotBlank(teacher.getSex()),Teacher::getSex,teacher.getSex());
        List<Teacher> teachers = teacherMapper.selectList(queryWrapper);
        PageInfo<Teacher> pageInfo = new PageInfo<>(teachers,11);
        return pageInfo;
    }

    @Override
    public Teacher login(String username, String password) {
        LambdaQueryChainWrapper<Teacher> chainWrapper = new LambdaQueryChainWrapper<>(teacherMapper);
        Teacher loginTeacher = chainWrapper
                .eq(Teacher::getUsername,username)
                .eq(Teacher::getPassword,password)
                .one();
        /*//更新最后登录时间 登录IP 登录次数
        Admin updateAdmin = new Admin();
        updateAdmin.setId(loginAdmin.getId());
        updateAdmin.setLoginIp(admin.getLoginIp());
        updateAdmin.setLoginTime(LocalDateTime.now());
        updateAdmin.setLoginCount(loginAdmin.getLoginCount()+1);
        //添加登录日志
        return updateAdmin;*/
        return loginTeacher;
    }

    @Override
    public List<Teacher> findList() {
        LambdaQueryChainWrapper<Teacher> chainWrapper = new LambdaQueryChainWrapper(teacherMapper);
        List<Teacher> teacherList = chainWrapper.eq(Teacher::getOnjob, 1)
                .list();
        return teacherList;
    }

    @Override
    public List<Integer> findAllId() {
        List<Integer> allId = teacherMapper.findAllId();
        return allId;
    }

    @Override
    public List<Role> findRoles(Integer id) {
        if(id==null){//id为空的话  返回一个空集合
            return Collections.emptyList();
        }
        //根据教师id查询 角色
        LambdaQueryWrapper<TeacherRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeacherRole::getTeacherId,id);
        List<TeacherRole> roles = teacherRoleMapper.selectList(queryWrapper);
        if(roles.isEmpty()){//如果角色为空的话  返回一个空集合
            return Collections.emptyList();
        }
        //取出所有role的id
        List<Integer> roleList = roles.stream()
                .map(role -> role.getRoleId())
                .collect(Collectors.toList());
        //返回 所有关于用户角色
        return roleMapper.selectBatchIds(roleList);
    }

    @Override
    public Boolean identityRepeat(String identity) {
        LambdaQueryChainWrapper<Teacher> chainWrapper = new LambdaQueryChainWrapper<>(teacherMapper);
        Integer count = chainWrapper.eq(Teacher::getIdentity, identity)
                .count();
        return count==0;
    }
    //后台管理员登录
    @Override
    public Teacher login(Teacher teacher) {
        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teacher::getUsername,teacher.getUsername());
        queryWrapper.eq(Teacher::getPassword,teacher.getPassword());
        return teacherMapper.selectOne(queryWrapper);
        //登录日志
        //...
    }

//    @Override
//    public  Teacher center(Integer id){
//        Teacher teacherCenter=teacherMapper.selectById(id);
//        return teacherCenter;
//    }

}
